<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2017/11/24
 * Time: 19:57
 */

namespace app\admin\model;


use think\Model;

class Address extends Model
{
    /**
     * 会员地址信息
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getAll(){

        $order="";
        $keywords   = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where = ['users.tel|users.full_name|users.username' => ['like' ,"%{$keywords}%" ]];
        }else{
            $where = [];
        }

        $address = $this
            ->alias('u_ad')
            ->join('user users','users.id=u_ad.member_id','LEFT')
            ->join('user user','users.rec_id=user.id','LEFT')
            ->join('area area','u_ad.area_code = area.code','LEFT')
            ->join('city city','area.citycode = city.code','LEFT')
            ->join('province pro','city.provincecode = pro.code','LEFT')
            ->join('auth_group group','users.user_auth=group.id','LEFT')
            ->field('u_ad.*,users.full_name,group.title,users.ctime,u_ad.address,
                    area.name as area_name,area.citycode,city.name as city_name
                    ,city.provincecode,pro.name as province_name')
            ->where($where)
            ->order($order)
            ->select();
        if($address){
            foreach($address as $k => $v){
                $address[$k]['location'] = $address[$k]['province_name'].$address[$k]['city_name'].$address[$k]['area_name'];
                $address[$k] = $v->toArray();
            }
        }
        return $address;
    }

    /**
     * 查询地址总记录数
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getCount(){
        $order="";
        $keywords   = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where['users.tel|users.full_name|users.username'] = array('like' ,"%{$keywords}%" );
        }else{
            $where = [];
        }
        $count = $this
            ->alias('u_ad')
            ->join('user users','users.id=u_ad.member_id','LEFT')
            ->join('user user','users.rec_id=user.id','LEFT')
            ->join('auth_group group','users.user_auth=group.id','LEFT')
            ->join('area area','u_ad.area_code = area.code','LEFT')
            ->join('city city','area.citycode = city.code','LEFT')
            ->join('province pro','city.provincecode = pro.code','LEFT')
            ->field('u_ad.*,users.username,users.full_name,users.user_images,
                            users.tel,users.sex,users.user.full_name as rec_name,
                            group.title,users.ctime,
                            u_ad.address,area.name as area_name,area.citycode,city.name as city_name
                    ,city.provincecode,pro.name as province_name')
            ->where($where)
            ->order($order)
            ->count();
        return $count;
    }

    /**
     * 查询详情
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getOne(){

        $keywords   = trim(input('get.keywords'));
        if(!empty($keywords)){
            $where['user.tel|user.full_name|user.username'] = array('like' ,"%{$keywords}%" );
        }else{
            $where = [];
        }

        $info  = $this
            ->alias("add")
            ->join('user u','u.id=add.member_id')
            ->join('auth_group group','u.user_auth=group.id','LEFT')
            ->join('area area','add.area_code = area.code','LEFT')
            ->join('city city','area.citycode = city.code','LEFT')
            ->join('province pro','city.provincecode = pro.code','LEFT')
            ->where($where)
            ->field("add.*,group.title,u.username,u.full_name,u.nickname,
                    u.tel,u.sex,u.age,area.name as area_name,area.citycode,city.name as city_name
                    ,city.provincecode,pro.name as province_name")
            ->find();
            if($info){
                $info = $info ->toArray();
            }
        return $info;
    }

    /**
     * 查询详情
     * @author 巫玉梅 <1120504614@qq.com>
     * @return
     */
    public function getAddressById($id){
        $info  = $this
            ->alias("add")
            ->join('user u','u.id=add.member_id')
            ->join('auth_group group','u.user_auth=group.id','LEFT')
            ->join('area area','add.area_code = area.code','LEFT')
            ->join('city city','area.citycode = city.code','LEFT')
            ->join('province pro','city.provincecode = pro.code','LEFT')
            ->where('add.id='.$id)
            ->field("add.*,group.title,u.username,u.full_name,u.nickname,
                    u.tel,u.sex,u.age,area.name as area_name,area.citycode,city.name as city_name
                    ,city.provincecode,pro.name as province_name")
            ->find();
        if($info){
            $info = $info ->toArray();
        }
        return $info;
    }

}